package com.ruoyi.sl.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.sl.domain.SlMonthOfc;
import com.ruoyi.sl.domain.SlMonthPay;
import com.ruoyi.sl.mapper.SlMonthOfcMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.sl.mapper.SlMonthWsMapper;
import com.ruoyi.sl.domain.SlMonthWs;
import com.ruoyi.sl.service.ISlMonthWsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 车间工资Service业务层处理
 * 
 * @author admin
 * @date 2024-06-16
 */
@Service
public class SlMonthWsServiceImpl implements ISlMonthWsService 
{
    @Autowired
    private SlMonthWsMapper slMonthWsMapper;
    @Autowired
    private SlMonthOfcMapper slMonthOfcMapper;

    /**
     * 查询车间工资
     * 
     * @param wsmpayId 车间工资主键
     * @return 车间工资
     */
    @Override
    public SlMonthWs selectSlMonthWsByWsmpayId(Long wsmpayId)
    {
        return slMonthWsMapper.selectSlMonthWsByWsmpayId(wsmpayId);
    }

    /**
     * 查询车间工资列表
     * 
     * @param slMonthWs 车间工资
     * @return 车间工资
     */
    @Override
    public List<SlMonthWs> selectSlMonthWsList(SlMonthWs slMonthWs)
    {
        return slMonthWsMapper.selectSlMonthWsList(slMonthWs);
    }

    /**
     * 条件查询车间工资列表
     *
     * @param slMonthWs 车间工资
     * @return 车间工资集合
     */
    @Override
    public List<SlMonthWs> selectSlMonthWsListByRange(SlMonthWs slMonthWs){
        return slMonthWsMapper.selectSlMonthWsListByRange(slMonthWs);
    }

    //根据日期姓名查询工资,返回格式转换为了计件和行政合并
    @Override
    public List<SlMonthPay> selectSlMonthWsByUserAndDate(String wsmpayYear, String wsmpayMonth, String userName){
        return slMonthWsMapper.selectSlMonthWsByUserAndDate(wsmpayYear,wsmpayMonth,userName);
    }

    //融合计件和行政月工资
    @Override
    public List<SlMonthPay> selectFusionMonthPay(SlMonthWs slMonthWs){
        List<SlMonthPay> mpayList = new ArrayList<>();
        Date tmpData = slMonthWs.getWsmpayDate();
        String year = null;
        String month = null;
        if (StringUtils.isNotNull(tmpData)){
            year = DateUtils.parseDateToStr("yyyy",tmpData);
            month = DateUtils.parseDateToStr("MM",tmpData);
        }
        mpayList.addAll(slMonthWsMapper.selectSlMonthWsByUserAndDate(year,month,slMonthWs.getUserName()));
        mpayList.addAll(slMonthOfcMapper.selectSlMonthOfcByUserAndDate(year,month,slMonthWs.getUserName()));
        return mpayList;

    }

    /**
     * 新增车间工资
     * 
     * @param slMonthWs 车间工资
     * @return 结果
     */
    @Override
    public int insertSlMonthWs(SlMonthWs slMonthWs)
    {
        slMonthWs.setCreateTime(DateUtils.getNowDate());
        return slMonthWsMapper.insertSlMonthWs(slMonthWs);
    }

    /**
     * 修改车间工资
     * 
     * @param slMonthWs 车间工资
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSlMonthWs(SlMonthWs slMonthWs)
    {
        slMonthWs.setUpdateTime(DateUtils.getNowDate());
        return updateWsMonthPay(slMonthWs);
        //return slMonthWsMapper.updateSlMonthWs(slMonthWs);
    }

    /**
     * 车间工资变为历史
     *
     * @return 结果
     */
    @Override
    public int updateSlMonthWsToHistory(){
        return slMonthWsMapper.updateSlMonthWsToHistory();
    }

    /**
     * 批量删除车间工资
     * 
     * @param wsmpayIds 需要删除的车间工资主键
     * @return 结果
     */
    @Override
    public int deleteSlMonthWsByWsmpayIds(Long[] wsmpayIds)
    {
        return slMonthWsMapper.deleteSlMonthWsByWsmpayIds(wsmpayIds);
    }

    /**
     * 删除车间工资信息
     * 
     * @param wsmpayId 车间工资主键
     * @return 结果
     */
    @Override
    public int deleteSlMonthWsByWsmpayId(Long wsmpayId)
    {
        return slMonthWsMapper.deleteSlMonthWsByWsmpayId(wsmpayId);
    }

    //根据日期删除工资
    @Override
    public int deleteSlMonthWsByDate(String wsmpayYear, String wsmpayMonth){
        return slMonthWsMapper.deleteSlMonthWsByDate(wsmpayYear,wsmpayMonth);
    }

    /**
     * 更新指定工资
     *
     * @param slMonthWs 工资信息
     * @return 结果
     */
    public int updateWsMonthPay(SlMonthWs slMonthWs){
        //绩效比例
        BigDecimal personPer = slMonthWs.getPersonalPerformance().divide(BigDecimal.valueOf(100)).setScale(2, BigDecimal.ROUND_DOWN);
        BigDecimal deptPer = slMonthWs.getDeptPerformance().divide(BigDecimal.valueOf(100)).setScale(2, BigDecimal.ROUND_DOWN);
        //发薪比例
        BigDecimal payPer = BigDecimal.valueOf(1).subtract(personPer).subtract(deptPer).setScale(2, BigDecimal.ROUND_DOWN);

        //早班次数
        int earlyNum = slMonthWs.getEarlyNum();
        //早班补助基数
        BigDecimal earlyBase = slMonthWs.getEarlyBase();
        //早班补助(基数x次数)
        slMonthWs.setEarlyShift(earlyBase.multiply(BigDecimal.valueOf(earlyNum)));
        //晚班次数
        int nightNum = slMonthWs.getNightNum();
        //晚班补助基数
        BigDecimal nightBase = (slMonthWs.getNightBase());
        //晚班补助(基数x次数)
        slMonthWs.setNightShift(nightBase.multiply(BigDecimal.valueOf(nightNum)));
        //加班补助（含绩效）
        BigDecimal overtimeAllallowance = slMonthWs.getOvertimeAllallowance();
        //加班补助
        //BigDecimal overtimeAllowance = overtimeAllallowance.multiply(payPer);
        //slMonthWs.setOvertimeAllowance(overtimeAllowance);
        //计件工资
        BigDecimal salary = slMonthWs.getBasicSalary();
        //出勤工龄
        BigDecimal ageAllpay = slMonthWs.getAgeAllpay();
        //餐补
        BigDecimal mealAllowance = slMonthWs.getMealAllowance();
        //高温补
        BigDecimal highTemperature = slMonthWs.getHighTemperature();
        //其他补贴
        BigDecimal otherSubsidy = slMonthWs.getOtherSubsidy();
        //应发工资=出勤固定+出勤工龄工资+餐补+高温补贴+早班补助+晚班补助+加班补助
        BigDecimal wagePayable = slMonthWs.getAttendancePay().add(ageAllpay).add(mealAllowance).add(highTemperature).add(slMonthWs.getEarlyShift()).add(slMonthWs.getNightShift()).add(slMonthWs.getOvertimeAllallowance()).add(otherSubsidy).setScale(2,RoundingMode.DOWN);
        //应发工资=计件分数*0.94+出勤工龄工资*0.94+餐补+早班补助*0.94+夜班补助*0.94+加班补助*0.94+高温补贴
        //BigDecimal wagePayable = salary.add(ageAllpay).add(slMonthWs.getEarlyShift()).add(slMonthWs.getNightShift()).add(slMonthWs.getOvertimeAllallowance()).multiply(payPer).add(mealAllowance).add(highTemperature);
        slMonthWs.setWagePayable(wagePayable);
        //每月实发=应发工资-公积金-社保-代扣税
        BigDecimal netSalary = wagePayable.subtract(slMonthWs.getProvidentFund()).subtract(slMonthWs.getSocialSecurity()).subtract(slMonthWs.getTaxDeduction()).setScale(2, BigDecimal.ROUND_DOWN);
        slMonthWs.setNetSalary(netSalary);
        slMonthWsMapper.updateSlMonthWs(slMonthWs);

        return 1;
    }

}
